import Vue from 'vue'
Vue.prototype.baseUrl = 'https://qiche.zjysdb.com/prod-api/'
// Vue.prototype.baseUrl = 'https://qiche.zjysdb.com/dev-api/'
// Vue.prototype.baseUrl = 'http://47.104.106.10:9380'
// request
let request = function ({
	method = "GET",
    url,
    data,
    success,
    fail,
    complete,
	showLoading
}) {
    // 请求
	var header = {
	 //    'content-type': 'application/json',
		// 'cache-control':'no-cache'
	}
	if(url!='/login'){
		header['Authorization'] = 'Bearer ' + uni.getStorageSync('token')
	}
	if (0 != url.indexOf("http")) {
        url = Vue.prototype.baseUrl + url
    }
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: method,
			data: data,
			header: header,
			dataType: 'json',
			success: success,
			fail: (res) => {
				uni.showToast({title:res.errMsg,icon:'none'})
				reject(res)
			},
			complete: (res) => {
				if(!showLoading){
					uni.hideLoading();
				}
				if (res.statusCode === 200) { //成功
					resolve(res.data)
					if (res.data.code === 401) { //登录失效
						uni.removeStorageSync('token')
						uni.reLaunch({
							url:'/pages/login'
						})
					} else if(res.data.code!==200 && res.data.code!==0){
						uni.showToast({title:res.data.msg,icon:'none'})
					} 
				} else {
					// uni.showToast({title:res.errMsg,icon:'none'})
					// reject(res)
				}
			}
		})
	});
};
export default request;
